<?php
namespace App\Modules\Relationship\Models;


use App\Http\Services\CommonServices;
use App\Modules\Relationship\Interfaces\ModelInterface;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use App\Http\Model\Base;

class CouplingRingRecordModel extends Base {

    // 戒指类型（0-经典戒指,1-荣誉戒指,2-等级戒指,3-其他戒指）
    protected $table = "coupling_ring_record";
    protected $fields = ['record_id as recordId', 'user_id as userId', 'ring_id as ringId',
        'type', 'expire_time as expireTime', 'created_at as createdAt',
        'updated_at as updatedAt','is_deleted as isDeleted'
    ];

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
        $this->tablePrefix = env('DB_PREFIX');
    }

    // 获取用户戒指
    public function getUserRing (int $userId, int $ringId) {
        $conition = array('user_id'=>$userId, 'ring_id'=>$ringId, 'is_deleted'=>0);
        return $this->getOneByCondition($conition, $this->fields);
    }

    // 获取一对CP全部戒指ID
    public function getAllRingIdByCP (array $userIds) {
        $table = $this->tablePrefix . $this->table;
        $ringsArr = array();
        $userIdsString = implode(',', $userIds);
        $sql = "select distinct ring_id as ringId from {$table} where user_id in ({$userIdsString}) and is_deleted = 0 and (expire_time > UNIX_TIMESTAMP(NOW()) or expire_time = 0) order by ring_id asc ";
        $rings = DB::select($sql);
        foreach ($rings as $ring) {
            $ringsArr[] = $ring->ringId;
        }
        return $ringsArr;
    }

    // 获取一对CP全部戒指ID
    public function getAllRingIdByUserId (int $userId) {
        $table = $this->tablePrefix . $this->table;
        $ringsArr = array();
        $sql = "select distinct ring_id as ringId from {$table} where user_id = {$userId} and is_deleted = 0 and (expire_time > UNIX_TIMESTAMP(NOW()) or expire_time = 0) order by ring_id asc ";
        $rings = DB::select($sql);
        foreach ($rings as $ring) {
            $ringsArr[] = $ring->ringId;
        }
        return $ringsArr;
    }

    // 写入用户戒指记录表
    public function insertOrUpdate($userId, $ringId, $type=0, $expireTime = 0):int {
        $table = $this->tablePrefix . $this->table;
        $record = $this->getOneByCondition(['user_id'=>$userId, 'ring_id'=>$ringId], $this->fields);
        $id = 0;
        if ($record) {
            $id = $record['recordId'];
            if ($expireTime != 0) {
                $sql = "UPDATE {$table} SET expire_time = expire_time + {$expireTime} where id={$id}";
                DB::update($sql);
            }
        } else {
             $id = DB::table($this->table)->insertGetId([
                'user_id' => $userId,
                'ring_id' => $ringId,
                'type' => $type,
                'expire_time' => $expireTime
            ]);
        }
        return $id;
    }

    public function getRingRecordById (int $id){
        $record = $this->getOneByCondition(array('record_id'=>$id), $this->fields);
        return $record;
    }

    public function deleteRing ($userId, $type) {
        return DB::table($this->table)->where('user_id',$userId)->whereIn('type', $type)->delete();
    }

}